﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ClassLib;
using BusinessLogic;
using System.Data;

namespace Interface.UserControls
{
    public partial class GoogleMap : System.Web.UI.UserControl
    {
        
        string mapCanvas = "map";        
        public string markerVariable = "marker";        
        GMapOptions gMapOptions = new GMapOptions();
        DataConnection dc = new DataConnection();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(LoadGoogleMapAPI());

            writer.WriteBeginTag("div");
            writer.WriteAttribute("ID", mapCanvas);
            writer.WriteAttribute("style", @"float:left; width:100%; height:300px;");
            writer.Write(Html32TextWriter.TagRightChar);
            writer.WriteEndTag("div");
            writer.Write(mapFocus());            

            base.Render(writer);
        }

        //public string mapVariable
        //{
        //    get { return strMapVariable; }
        //    set
        //    {
        //        if (!String.IsNullOrEmpty(value))
        //        {
        //            try
        //            {
        //                strMapVariable = value.ToString();
        //            }
        //            catch { }
        //        }
        //    }
        //}

        

        public string LoadGoogleMapAPI()
        {
            StringBuilder strbd = new StringBuilder();
            strbd.Append(@"<script type=""text/javascript"" src=""http://maps.google.com/maps/api/js?sensor=false""> </script>" + Environment.NewLine + Environment.NewLine);            
            return strbd.ToString();
        }

        public string MapScript(string strLat, string strLong)
        {
            System.Text.StringBuilder script = new System.Text.StringBuilder();
            script.Append(@"<script language=""javascript"" type=""text/javascript"">" + Environment.NewLine + Environment.NewLine);
            script.Append(@"(function() {" + Environment.NewLine);
            script.Append(@"window.onload = function(){" + Environment.NewLine);
            script.Append(@"var latlng = new google.maps.LatLng(" + strLat + ", " + strLong + ");" + Environment.NewLine);            
            script.Append(@"var options = " + gMapOptions.RequiredMapOptions(strLat, strLong) + ";" + Environment.NewLine);
            script.Append(@"var " + mapCanvas + @" = new google.maps.Map(document.getElementById(""" + mapCanvas + @"""), options);" + Environment.NewLine);

            script.Append(@"var marker = new google.maps.Marker({" + Environment.NewLine);
            script.Append(@"position: new google.maps.LatLng(" + strLat + "," + strLong + "), map: map" + Environment.NewLine);
            script.Append(@"});" + Environment.NewLine);

            script.Append(@"}})();" + Environment.NewLine);
            script.Append(@"</script>" + Environment.NewLine);

            return script.ToString();
        }

        public string mapFocus()
        {
            string strMap = "";
            string houseID = HttpUtility.UrlDecode(Request.QueryString.Get("ID"));
            if (!string.IsNullOrEmpty(houseID))
            {
                string sql = "Select * from BoardingHouse where IdHouse='" + houseID + "'";
                DataTable dt = dc.getData(sql);
                if (dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    string strLat = row["Lat"].ToString();
                    string strLong = row["Long"].ToString();
                    strMap = MapScript(strLat, strLong);
                }
            }
            return strMap;
        }
                    
    }
}